home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MISC.SWG / 0016_TPASM.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  10KB  |  266 lines

  1. {  Ok here it is..   I have disasembled the following TP Program to
  2. show you the inner workings of TP (well at least 6.0).  The
  3. Folloing Program was Compiled in the IDE With RANGE, I/O, STACK
  4. checking turned off.  Look at the code close and see if you can
  5. find a nasty little bug in it beFore I show you the Asm that TP
  6. Created on disk.
  7. }
  8.  
  9. Program TstFiles;
  10.  
  11. Type MyRec = Record
  12.                LInt : LongInt;
  13.                Hi   : Word;
  14.                Lo   : Word;
  15.                B1   : Byte;
  16.                B2   : Byte;
  17.                B3   : Byte;
  18.                B4   : Byte;
  19.              end;            {Record Size 12 Bytes}
  20.  
  21. Const MaxRecs = 100;
  22.  
  23.  
  24. Var MyTypedFile   : File of MyRec;
  25.     MyUnTypedFile : File;
  26.  
  27.     Rec           : MyRec;
  28.     RecCnt        : Word;
  29.  
  30.  
  31. Procedure FillRec (RecSeed : LongInt);
  32.  
  33.   begin
  34.   Rec.Lint := RecSeed;
  35.   Rec.Hi   := Hi (Rec.Lint);
  36.   Rec.Lo   := Lo (Rec.Lint);
  37.   Rec.B1   := Lo (Rec.Lo);
  38.   Rec.B2   := Hi (Rec.Lo);
  39.   Rec.B3   := Lo (Rec.Hi);
  40.   Rec.B4   := Hi (Rec.Hi);
  41.   end;
  42.  
  43.  
  44.  
  45.  
  46. begin
  47. Assign  (MyTypedFile,   'Type.Dat');
  48. Assign  (MyUnTypedFile, 'UnTyped.Dat');
  49. ReWrite (MyTypedFile);
  50. ReWrite (MyUnTypedFile);
  51.  
  52. For RecCnt := 1 to MaxRecs do
  53.   begin
  54.   FillRec (RecCnt);
  55.  
  56.   Write (MyTypedFile  , Rec);
  57. { Write (MyUnTypedFile, Rec);} {Illegal can't do this}
  58.  
  59.   FillRec (RecCnt + $FFFF);
  60.  
  61. { BlockWrite (MyTypedFile, Rec, 1);} {Illegal Can't do this eather}
  62.  
  63.   BlockWrite (MyUnTypedFile, Rec, Sizeof (MyRec));
  64.   end;
  65.  
  66.  
  67. end.
  68.  
  69.  
  70. The Asm Break down is in the next two messages...
  71.  
  72. TSTFileS.38: begin
  73.   cs:0051 9A0000262D     call   2D26:0000 <-------TP Start Up Code
  74.   cs:0056 55             push   bp
  75.   cs:0057 89E5           mov    bp,sp
  76. TSTFileS.39: Assign (MyTypedFile, 'Type.Dat');
  77.   cs:0059 BF4400         mov    di,0044
  78.   cs:005C 1E             push   ds
  79.   cs:005D 57             push   di
  80.   cs:005E BF3C00         mov    di,003C
  81.   cs:0061 0E             push   cs
  82.   cs:0062 57             push   di
  83.   cs:0063 9AC004262D     call   2D26:04C0 <-------TP's Routine to set
  84.                                                   up File Records.
  85. TSTFileS.40: Assign (MyUnTypedFile, 'UnTyped.Dat');
  86.   cs:0068 BFC400         mov    di,00C4
  87.   cs:006B 1E             push   ds
  88.   cs:006C 57             push   di
  89.   cs:006D BF4500         mov    di,0045
  90.   cs:0070 0E             push   cs
  91.   cs:0071 57             push   di
  92.   cs:0072 9AC004262D     call   2D26:04C0 <-------TP's Routine to set
  93.                                                   up File Records.
  94. TSTFileS.41: ReWrite (MyTypedFile);
  95.   cs:0077 BF4400         mov    di,0044
  96.   cs:007A 1E             push   ds
  97.   cs:007B 57             push   di
  98.   cs:007C B80C00         mov    ax,000C
  99.   cs:007F 50             push   ax
  100.   cs:0080 9AF704262D     call   2D26:04F7 <-------TP's Routine to
  101.                                                   Create File.
  102. TSTFileS.42: ReWrite (MyUnTypedFile);
  103.   cs:0085 BFC400         mov    di,00C4
  104.   cs:0088 1E             push   ds
  105.   cs:0089 57             push   di
  106.   cs:008A B88000         mov    ax,0080
  107.   cs:008D 50             push   ax
  108.   cs:008E 9AF704262D     call   2D26:04F7 <-------TP's Routine to
  109.                                                   Create File.
  110. TSTFileS.44: For RecCnt := 1 to MaxRecs do
  111.   cs:0093 C70650010100   mov    Word ptr [TSTFileS.RECCNT],00
  112.     ***  Clear the loop counter For first loop
  113.   cs:0099 EB04           jmp    TSTFileS.46 (009F)
  114.     ***  Jump to the start of the Loop
  115.   cs:009B FF065001       inc    Word ptr [TSTFileS.RECCNT]
  116.     ***  The Loop returns to here to inC the loop counter
  117. TSTFileS.46:  FillRec (RecCnt);
  118.   cs:009F A15001         mov    ax,[TSTFileS.RECCNT]
  119.     ***  Move our RecCnt Var into AX register
  120.   cs:00A2 31D2           xor    dx,dx
  121.     ***  Clear the DX Register
  122.   cs:00A4 52             push   dx
  123.   cs:00A5 50             push   ax
  124.     ***  Push the DX and AX Registers on the stack.  Remember our
  125.          FillRec Routine expects a LongInt to be passed and RecCnt
  126.          is only a Word.  So it Pushes the DX as the 0 Upper Word
  127.          of the LongInt.
  128.   cs:00A6 0E             push   cs
  129.     ***  Push the code segment For some reasion.
  130.   cs:00A7 E856FF         call   TSTFileS.FILLREC
  131.     ***  Call our FillRec Routine
  132. TSTFileS.48:  Write (MyTypedFile , Rec);
  133.   cs:00AA BF4400         mov    di,0044
  134.   cs:00AD 1E             push   ds
  135.   cs:00AE 57             push   di
  136.     ***  These instructions push the address of MyTypedFile Record
  137.          on the stack.  The first paramiter
  138.   cs:00AF BF4401         mov    di,0144
  139.   cs:00B2 1E             push   ds
  140.   cs:00B3 57             push   di
  141.     ***  These instructions push the address of Rec Record
  142.          on the stack.  The second paramiter
  143.   cs:00B4 9AAA05262D     call   2D26:05AA
  144.     ***  Call the System Function to Write a Typed File.  (In next msg)
  145.   cs:00B9 83C404         add    sp,0004
  146.     ***  Remove our passed parameters from the stack
  147. TSTFileS.51:  FillRec (RecCnt + $FFFF);
  148.   cs:00BC A15001         mov    ax,[TSTFileS.RECCNT]
  149.   cs:00BF 05FFFF         add    ax,FFFF
  150.   cs:00C2 31D2           xor    dx,dx
  151.   cs:00C4 52             push   dx
  152.   cs:00C5 50             push   ax
  153.   cs:00C6 0E             push   cs
  154.   cs:00C7 E836FF         call   TSTFileS.FILLREC
  155.     ***  Now heres a NASTY littel bug With the code!!!  Look at the
  156.          above routine.  We wanted to pass a LongInt $FFFF + rec cnt
  157.          But we wound up adding the $FFFF to a Word then passing a
  158.          LongInt.  if you Compile the sample pas File you'll be able
  159.          to see this bug in action..  Good reasion to use a Debugger.
  160. TSTFileS.55:  BlockWrite (MyUnTypedFile, Rec, Sizeof (MyRec))
  161.   cs:00CA BFC400         mov    di,00C4
  162.   cs:00CD 1E             push   ds
  163.   cs:00CE 57             push   di
  164.     ***  These instructions push the address of MyUnTypeFile Record
  165.          on the stack.  The First paramiter
  166.   cs:00CF BF4401         mov    di,0144
  167.   cs:00D2 1E             push   ds
  168.   cs:00D3 57             push   di
  169.   cs:0594 26817D02B3D7   cmp    es:Word ptr [di+02],D7B3
  170.     *** Armed With the address of the File Record in ES:DI
  171.         Check the File mode For a In/Out operation.  See Dos
  172.         Unit Constant definitions.
  173.   cs:059A 7406           je     05A2
  174.     *** if that Compare was equal then jump to return
  175.   cs:059C C7063C006700   mov    Word ptr [SYSTEM.inOUTRES],0069
  176.     *** if we didn't jump then put File not oopen For output in
  177.         Ioresult.
  178.   cs:05A2 C3             ret
  179.     *** Go back to where we were called
  180.   cs:05A3 B43F           mov    ah,3F
  181.   cs:05A5 BA6400         mov    dx,0064
  182.   cs:05A8 EB05           jmp    05AF
  183.  
  184.     *** The Write instruction entered the system Unit here
  185.   cs:05AA B440           mov    ah,40
  186.     *** Load Dos Function in AH
  187.   cs:05AC BA6500         mov    dx,0065
  188.     *** Default error code 101 disk Write error load in DX
  189.   cs:05AF 55             push   bp
  190.     ***  Save the BP register
  191.   cs:05B0 8BEC           mov    bp,sp
  192.     *** Load the BP Register With the stack Pointer
  193.   cs:05B2 C47E0A         les    di,[bp+0A]
  194.     *** Load Address of MyTypeFile Rec in ES:SI
  195.   cs:05B5 E8DCFF         call   0594
  196.     *** Call check For File mode.  See top of message
  197.   cs:05B8 751B           jne    05D5
  198.     *** if error jump out of this
  199.   cs:05BA 1E             push   ds
  200.   cs:05BB 52             push   dx
  201.     *** Save These Registers as we'er going to use them
  202.   cs:05BC C55606         lds    dx,[bp+06]
  203.     *** Load the address of our Rec in DS:DX Registers
  204.   cs:05BF 268B4D04       mov    cx,es:[di+04]
  205.     *** Look up Record structure For a File Rec and you'll see
  206.         that RecSize is Byte # 4.  Move that value to CX
  207.   cs:05C3 268B1D         mov    bx,es:[di]
  208.     *** First Byte of a File Rec is the Handel.  Move into BX
  209.   cs:05C6 CD21           int    21
  210.     *** Make the Dos CALL to Write.  AH = 40
  211.                                      BX = File Handel
  212.                                      CX = # of Bytes to Write.
  213.                                      DS:DX = Address of Buffer
  214.         Returns Error In AX if Carry flag set or
  215.         if good CF = 0 number of Bytes written in AX
  216.   cs:05C8 5A             pop    dx
  217.   cs:05C9 1F             pop    ds
  218.     *** Restore the Registers
  219.   cs:05CA 7206           jb     05D2
  220.     *** Jump if there was an error (if Carry flag Set)
  221.   cs:05CC 3BC1           cmp    ax,cx
  222.     *** Comp Bytes requested to what was written
  223.   cs:05CE 7405           je     05D5
  224.     *** if equal then jump out we'r just about done
  225.   cs:05D0 8BC2           mov    ax,dx
  226.     *** Move default errorcode 101 to AX
  227.   cs:05D2 A33C00         mov    [SYSTEM.inOUTRES],ax <--Set Ioresult
  228.     *** Store 101 to Ioresult
  229.   cs:05D5 5D             pop    bp
  230.     *** Restore BP register
  231.   cs:05D6 CA0400         retf   0004
  232.     *** We'r out of here
  233.  
  234.   cs:05D9 B33F           mov    bl,3F
  235.   cs:05DB B96400         mov    cx,0064
  236.   cs:05DE EB05           jmp    05E5
  237.  
  238.  
  239.     *** The BlockWrite instruction entered the system Unit here
  240.   cs:05E0 B340           mov    bl,40
  241.     *** Move Dos Function in BL
  242.   cs:05E2 B96500         mov    cx,0065
  243.     *** Default error 101 Write error in CX
  244.   cs:05E5 55             push   bp
  245.     *** Save BP Register
  246.   cs:05E6 8BEC           mov    bp,sp
  247.     *** Move Stack Pointer to BP
  248.   cs:05E8 C47E10         les    di,[bp+10]
  249.     *** Load Address of MyUnTypedFile Record in ES:DI
  250.   cs:05EB E8A6FF         call   0594
  251.     *** Check For Open in Write Mode See top of message
  252.   cs:05EE 753F           jne    062F
  253.     *** Jump if not in Write mode
  254.   cs:05F0 8B460A         mov    ax,[bp+0A] ]
  255.     *** Move File Record cnt in to ax
  256.   cs:05F3 0BC0           or     ax,ax
  257.     *** Check For 0 Record request
  258.   cs:05F5 741C           je     0613
  259.     *** Jump if 0 rec requested
  260.   cs:05F7 1E             push   ds
  261.   cs:05F8 51             push   cx
  262.     *** Save them we'er going to use them
  263.   cs:05F9 26F76504       mul    es:Word ptr [di+04]
  264.     *** Multiply Record size With RecCnt in AX result in DX & AX
  265.   cs:05FD 8BC8           mov    cx,ax
  266.